// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: ST Logo Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.sonicFrameTablePos
private alias object.value2 : object.tailsFrameTablePos
private alias object.value3 : object.levelSelectCheatPos
private alias object.value4 : object.regionSwitchCheatPos

// States
private alias 0  : STLOGO_SETUP
private alias 1  : STLOGO_FADEIN
private alias 2  : STLOGO_SHOWEMBLEM
private alias 3  : STLOGO_ENTERSONIC
private alias 4  : STLOGO_ENTERSONICHAND
private alias 5  : STLOGO_SHOWSONICALONE
private alias 6  : STLOGO_ENTERTAILS
private alias 7  : STLOGO_ENTERTAILSHAND
private alias 8  : STLOGO_SHOWSONICTAILS
private alias 9  : STLOGO_SHOWBG
private alias 10 : STLOGO_FINISHEDANIMATING

// ShootingStar Aliases
private alias object.value0 : shootingStar.delay

// Game Modes
private alias 0 : MODE_NOSAVE

// Player List Pos Aliases
private alias 0 : PLAYER_SONIC_A
private alias 3 : PLAYER_SONIC_TAILS_A
private alias 5 : PLAYER_AMY_A
private alias 6 : PLAYER_AMY_TAILS_A


// ========================
// Function Declarations
// ========================

reserve function STLogo_CheckButtonCheatCodes


// ========================
// Tables
// ========================

private table STLogo_sonicFrameDurationTable
	4, 4, 4, 4, 4, 4, 4, 7, 2, 2, 3, 4, 4, 4, 4
end table

private table STLogo_tailsFrameDurationTable
	4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4
end table


// ========================
// Function Definitions
// ========================

public function STLogo_CheckButtonCheatCodes
	// This function is used by the [K Logo] Object too
	
	temp0 = 0
	if keyPress[0].up == true
		temp0 = 1
	end if

	if keyPress[0].down == true
		temp0 = 2
	end if

	if keyPress[0].left == true
		temp0 = 3
	end if

	if keyPress[0].right == true
		temp0 = 4
	end if

	if keyPress[0].buttonA == true
		temp0 = 5
	end if

	if keyPress[0].buttonB == true
		temp0 = 6
	end if

	if keyPress[0].buttonC == true
		temp0 = 7
	end if

	// Level Select Cheat
	// 1,  1,  1,  2,    2,    2,    3,    4,     3,    4
	// up, up, up, down, down, down, left, right, left, right
	switch object.levelSelectCheatPos
	case 0
		if temp0 == 1
			object.levelSelectCheatPos++
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break

	case 1
		if temp0 == 1
			object.levelSelectCheatPos++
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break

	case 2
		if temp0 == 1
			object.levelSelectCheatPos++
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break

	case 3
		if temp0 == 2
			object.levelSelectCheatPos++
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break

	case 4
		if temp0 == 2
			object.levelSelectCheatPos++
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break

	case 5
		if temp0 == 2
			object.levelSelectCheatPos++
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break

	case 6
		if temp0 == 3
			object.levelSelectCheatPos++
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break

	case 7
		if temp0 == 4
			object.levelSelectCheatPos++
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break

	case 8
		if temp0 == 3
			object.levelSelectCheatPos++
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break

	case 9
		if temp0 == 4
			object.levelSelectCheatPos++
			object.levelSelectCheatPos = 0
			options.stageSelectFlag = true
			PlaySfx(SfxName[Ring L], false)
		else
			if temp0 != 0
				object.levelSelectCheatPos = 0
			end if
		end if
		break
		
	end switch


	// Region Switch Cheat
	// 1,  2,    2,    2,    2,    1
	// up, down, down, down, down, up
	switch object.regionSwitchCheatPos
	case 0
		if temp0 == 1
			object.regionSwitchCheatPos++
		else
			if temp0 != 0
				object.regionSwitchCheatPos = 0
			end if
		end if
		break

	case 1
	case 2
	case 3
	case 4
		if temp0 == 2
			object.regionSwitchCheatPos++
		else
			if temp0 != 0
				object.regionSwitchCheatPos = 0
			end if
		end if
		break

	case 5
		if temp0 == 1
			object.regionSwitchCheatPos++
			object.regionSwitchCheatPos = 0
			options.region ^= 1
			PlaySfx(SfxName[Ring L], false)
		else
			if temp0 != 0
				object.regionSwitchCheatPos = 0
			end if
		end if
		break
	end switch
end function


// ========================
// Events
// ========================

event ObjectUpdate
#platform: USE_STANDALONE
	if stage.player2Enabled == true
		stage.playerListPos = PLAYER_SONIC_TAILS_A
	end if
#endplatform
	
#platform: USE_ORIGINS
	if object.state <= STLOGO_FADEIN
		switch stage.playerListPos
		case PLAYER_AMY_A
			stage.playerListPos = PLAYER_SONIC_A
			break
			
		case PLAYER_AMY_TAILS_A
			stage.playerListPos = PLAYER_SONIC_TAILS_A
			break
		end switch
	end if
#endplatform
	
	if options.gameMode == MODE_NOSAVE
		if object.state > STLOGO_SHOWEMBLEM
			CallFunction(STLogo_CheckButtonCheatCodes)
		end if
	end if
	
	switch object.state
	case STLOGO_SETUP
		if object.timer == 30
			object[+1].type = TypeName[Twinkle Stars]
			object[+1].priority = PRIORITY_ACTIVE
		end if

		if object.timer < 60
			object.timer++
		else
			object.timer = 0
			object.alpha = 0x100
			object.state++
		end if
		break

	case STLOGO_FADEIN
		if object.alpha > 0
			object.alpha -= 8
		else
			object.state++
		end if
		break

	case STLOGO_SHOWEMBLEM
		if object.timer < 32
			object.timer++
		else
			object.timer = 0
			PlayMusic(0)
			object.state++
		end if
		break

	case STLOGO_ENTERSONIC
		if object.animationTimer < 2
			object.frame = object.sonicFrameTablePos
			object.frame += 2
			GetTableValue(object.animationTimer, object.sonicFrameTablePos, STLogo_sonicFrameDurationTable)

			object.sonicFrameTablePos++
			if object.sonicFrameTablePos == 12
				object.frame++
				object.state++
			end if
		else
			object.animationTimer--
		end if
		break

	case STLOGO_ENTERSONICHAND
		if object.animationTimer < 2
			object.frame = object.sonicFrameTablePos
			object.frame += 3
			GetTableValue(object.animationTimer, object.sonicFrameTablePos, STLogo_sonicFrameDurationTable)

			object.sonicFrameTablePos++
			if object.sonicFrameTablePos == 16
				object.animationTimer = 0
				object.state++
			end if
		else
			object.animationTimer--
		end if
		break

	case STLOGO_SHOWSONICALONE
		if object.timer < 4
			object.timer++
		else
			object.timer = 0
			object.state++
		end if
		break

	case STLOGO_ENTERTAILS
		if object.animationTimer < 2
			object.frame = object.tailsFrameTablePos
			object.frame += 18
			GetTableValue(object.animationTimer, object.tailsFrameTablePos, STLogo_tailsFrameDurationTable)

			object.tailsFrameTablePos++
			if object.tailsFrameTablePos == 11
				object.frame++
				object.state++
			end if
		else
			object.animationTimer--
		end if
		break

	case STLOGO_ENTERTAILSHAND
		if object.animationTimer < 2
			object.frame = object.tailsFrameTablePos
			object.frame += 19
			GetTableValue(object.animationTimer, object.tailsFrameTablePos, STLogo_tailsFrameDurationTable)

			object.tailsFrameTablePos++
			if object.tailsFrameTablePos == 14
				object.animationTimer = 0
				object.state++
			end if
		else
			object.animationTimer--
		end if
		break

	case STLOGO_SHOWSONICTAILS
		if object.timer < 30
			object.timer++
		else
			object.timer = 0
			object.alpha = 256
			stage.activeLayer[0] = 2 // Show Title BG
			object.state++
		end if
		break

	case STLOGO_SHOWBG
		if object.alpha > 0
			object.alpha -= 12
		else
			object.state++

			object[+1].type = TypeName[Start Message]
			object[+1].priority = PRIORITY_ACTIVE
			object[+1].xpos = screen.xcenter
			object[+1].ypos = 208

			CreateTempObject(TypeName[Shooting Star], 0, object.xpos, object.ypos)
			object[tempObjectPos].drawOrder = 2
			object[tempObjectPos].xpos += 0x1600000
			object[tempObjectPos].ypos -= 0x1000000
			shootingStar[tempObjectPos].delay = 90
		end if
		break

	case STLOGO_FINISHEDANIMATING
		object.timer++
		if object.timer > 7
			tileLayer[2].deformationOffset++
			object.timer = 0
		end if
		break
		
	end switch

	temp0 = screen.xcenter
	FlipSign(temp0)
	temp0 += 256
	screen.xoffset = temp0
end event


event ObjectDraw
	switch object.state
	case STLOGO_SETUP
		ClearScreen(1)
		break

	case STLOGO_FADEIN
		ClearScreen(1)

		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Emblem (Bottom)
		DrawSprite(1)

		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, object.alpha)
		break

	case STLOGO_SHOWEMBLEM
		ClearScreen(1)

		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Emblem (Bottom)
		DrawSprite(1)
		break

	case STLOGO_ENTERSONIC
		ClearScreen(1)

		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Sonic
		DrawSprite(object.frame)

		// Draw Emblem (Bottom)
		DrawSprite(1)
		break

	case STLOGO_ENTERSONICHAND
		ClearScreen(1)

		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Sonic
		DrawSprite(13) // Sonic
		DrawSprite(object.frame) // Hand

		// Draw Emblem (Bottom)
		DrawSprite(1)
		break

	case STLOGO_SHOWSONICALONE
		ClearScreen(1)

		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Sonic
		DrawSprite(13) // Sonic
		DrawSprite(17) // Hand

		// Draw Emblem (Bottom)
		DrawSprite(1)
		break

	case STLOGO_ENTERTAILS
		ClearScreen(1)

		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Tails
		DrawSprite(object.frame) // Tails

		// Draw Sonic
		DrawSprite(13) // Sonic
		DrawSprite(17) // Hand

		// Draw Emblem (Bottom)
		DrawSprite(1)
		break

	case STLOGO_ENTERTAILSHAND
		ClearScreen(1)

		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Tails
		DrawSprite(28) // Tails
		DrawSprite(object.frame) // Hand

		// Draw Sonic
		DrawSprite(13) // Sonic
		DrawSprite(17) // Hand

		// Draw Emblem (Bottom)
		DrawSprite(1)
		break

	case STLOGO_SHOWSONICTAILS
		ClearScreen(1)

		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Tails
		DrawSprite(28) // Tails
		DrawSprite(31) // Hand

		// Draw Sonic
		DrawSprite(13) // Sonic
		DrawSprite(17) // Hand

		// Draw Emblem (Bottom)
		DrawSprite(1)
		break

	case STLOGO_SHOWBG
		DrawRect(0, 0, screen.xsize, screen.ysize, 0xE0, 0xE0, 0xE0, object.alpha)

		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Tails
		DrawSprite(28) // Tails
		DrawSprite(31) // Hand

		// Draw Sonic
		DrawSprite(13) // Sonic
		DrawSprite(17) // Hand

		// Draw Emblem (Bottom)
		DrawSprite(1)

		// Draw Copyright
		temp0 = screen.xsize
		temp0 -= 96
		DrawSpriteScreenXY(32, temp0, 216)
		break

	case STLOGO_FINISHEDANIMATING
		// Draw Emblem (Top)
		DrawSprite(0)

		// Draw Tails
		DrawSprite(28) // Tails
		DrawSprite(31) // Hand

		// Draw Sonic
		DrawSprite(13) // Sonic
		DrawSprite(17) // Hand

		// Draw Emblem (Bottom)
		DrawSprite(1)

		// Draw Copyright
		temp0 = screen.xsize
		temp0 -= 96
		DrawSpriteScreenXY(32, temp0, 216)
		break
		
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("Title/Title.gif")
	SetMusicTrack("Titlescreen.ogg", 0, false)

	arrayPos0 = 0x100
	while arrayPos0 < 0x240
		arrayPos1 = arrayPos0
		arrayPos1 -= 0x100
		Rand(stage.deformationData2[arrayPos1], 4)
		temp0 = arrayPos0
		temp0 &= 1
		if temp0 == 1
			FlipSign(stage.deformationData2[arrayPos1])
		end if
		stage.deformationData2[arrayPos0] = stage.deformationData2[arrayPos1]
		arrayPos0++
	loop

	SpriteFrame(-128, -78, 256, 82, 255, 67)	// Emblem (Top) 			- #0
	SpriteFrame(-128, -8, 256, 87, 255, 150)	// Emblem (Bottom) 			- #1
	SpriteFrame(-21, -19, 72, 40, 1, 66)		// Sonic - Frame 1 			- #2
	SpriteFrame(-29, -35, 72, 67, 1, 66)		// Sonic - Frame 2 			- #3
	SpriteFrame(-37, -51, 72, 67, 1, 66)		// Sonic - Frame 3 			- #4
	SpriteFrame(-39, -61, 72, 67, 1, 66)		// Sonic - Frame 4 			- #5
	SpriteFrame(-35, -69, 72, 67, 1, 66)		// Sonic - Frame 5 			- #6
	SpriteFrame(-29, -73, 72, 67, 1, 66)		// Sonic - Frame 6 			- #7
	SpriteFrame(-25, -74, 72, 67, 1, 66)		// Sonic - Frame 7 			- #8
	SpriteFrame(-21, -75, 72, 67, 1, 66)		// Sonic - Frame 8 			- #9
	SpriteFrame(-18, -76, 74, 84, 74, 66)		// Sonic - Frame 9 			- #10
	SpriteFrame(-3, -76, 74, 71, 149, 66)		// Sonic - Frame 10 		- #11
	SpriteFrame(0, -79, 71, 74, 1, 134)	  		// Sonic - Frame 11 		- #12
	SpriteFrame(0, -79, 71, 74, 74, 151)  		// Sonic - Frame 12 		- #13
	SpriteFrame(45, -49, 30, 43, 149, 138)		// Sonic Hand - Frame 1 	- #14
	SpriteFrame(43, -47, 30, 43, 149, 138)		// Sonic Hand - Frame 2 	- #15
	SpriteFrame(40, -46, 30, 43, 149, 138)		// Sonic Hand - Frame 3 	- #16
	SpriteFrame(41, -47, 30, 43, 149, 138)		// Sonic Hand - Frame 4 	- #17
	SpriteFrame(-49, -24, 56, 64, 1, 1)			// Tails - Frame 1 			- #18
	SpriteFrame(-53, -40, 56, 64, 1, 1)			// Tails - Frame 2 			- #19
	SpriteFrame(-58, -52, 56, 64, 1, 1)			// Tails - Frame 3 			- #20
	SpriteFrame(-60, -58, 56, 64, 1, 1)			// Tails - Frame 4 			- #21
	SpriteFrame(-62, -62, 56, 64, 1, 1)			// Tails - Frame 5 			- #22
	SpriteFrame(-63, -63, 56, 64, 1, 1)			// Tails - Frame 6 			- #23
	SpriteFrame(-64, -64, 56, 64, 1, 1)			// Tails - Frame 7 			- #24
	SpriteFrame(-86, -59, 89, 57, 58, 1)		// Tails - Frame 8 			- #25
	SpriteFrame(-80, -64, 88, 61, 149, 1)		// Tails - Frame 9 			- #26
	SpriteFrame(-82, -63, 89, 59, 238, 1)		// Tails - Frame 10			- #27
	SpriteFrame(-83, -68, 87, 65, 328, 1)		// Tails - Frame 11			- #28
	SpriteFrame(-9, -22, 16, 19, 417, 1)		// Tails Hand - Frame 1 	- #29
	SpriteFrame(-12, -27, 16, 19, 417, 1)		// Tails Hand - Frame 2 	- #30
	SpriteFrame(-11, -26, 16, 19, 417, 1)		// Tails Hand - Frame 3 	- #31
	SpriteFrame(0, 0, 88, 16, 1, 421)			// Copyright 				- #32
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
	DrawSprite(1)
end event


event RSDKLoad
	LoadSpriteSheet("Title/Title.gif")
	SpriteFrame(-128, -78, 256, 82, 255, 67)	// Emblem (Top) 			- #0
	SpriteFrame(-128, -8, 256, 87, 255, 150)	// Emblem (Bottom) 			- #1
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
